﻿using System;
using System.Collections.Generic;
namespace IFramework
{
    public class PoolObjBoxCreater
    {
        private Queue<PoolObjBox> boxes = new Queue<PoolObjBox>();
        private int capcity;
        public PoolObjBoxCreater(int capcity)
        {
            this.capcity = capcity;
        }
        public PoolObjBox GetBox(IPoolObj poolObj, object objParent, Type objType)
        {
            if (boxes.Count == 0)
                return new PoolObjBox(poolObj, objParent, objType);
            PoolObjBox box = boxes.Dequeue();
            box.SetValue(poolObj, objParent, objType);
            return box;
        }
        public void RestoreBox(PoolObjBox box)
        {
            boxes.Enqueue(box);
            if (boxes.Count > capcity * 2)
                AutoClean(true);
        }
        private void AutoClean(bool isHalf)
        {
            if (isHalf)
            {
                while (boxes.Count > capcity * 2)
                {
                    boxes.Dequeue();
                }
            }
            else
            {
                while (boxes.Count > 0)
                {
                    boxes.Dequeue();
                }
            }
        }
        public void Clean()
        {
            AutoClean(false);
        }
    }
}
